<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .active {
            background: #ff4400;
            color: #fff;
        }
    </style>
</head>

<body>
    <div id="app">
        <input type="text" v-model="search" @keydown.down="down" @keydown.up.prevent="up" @keydown.enter="enter">
        <ul>
            <li v-for="(item,index) in shArr" :key="item[0]" :class="index===n?'active':''">{{item[0]}}</li>
        </ul>
    </div>
</body>

</html>
<script src="vue.js"></script>
<script>
    // https://suggest.taobao.com/sug?q=sk2&callback=aa&code=utf-8
    let vm = new Vue({
        el: "#app",
        data: {
            search: "",
            shArr: [],
            n: -1
        },
        // 监听数据，发送请求
        watch: {
            search() {
                if (this.search === "") {
                    this.shArr = [];
                    return;
                }
                let os = document.createElement("script");
                os.src = `https://suggest.taobao.com/sug?q=${this.search}&callback=aa&code=utf-8`;
                document.body.appendChild(os);
            }
        },
        methods: {
            down() {
                this.n++;
                if (this.n > 9) {
                    this.n = -1;
                }
            },
            up() {
                this.n--;
                if (this.n < 0) {
                    this.n = 10;
                }
            },
            enter() {
                if (this.n >= 0 && this.n <= 3) {
                    window.open("https://www.baidu.com/s?wd=" + this.shArr[this.n][0], "_self")
                } else {
                    window.open("https://www.baidu.com/s?wd=" + this.search, "_self")
                }
            }
        }

    })
    function aa(a) {
        console.log(a.result);
        vm.shArr = a.result;
    }
</script>